package org.huangrui.spark.java.core.acc;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.util.LongAccumulator;

import java.util.Arrays;

/**
 * @Author hr
 * @Create 2024-10-19 21:45
 */
public class Spark02_Acc {
    public static void main(String[] args) {
        final SparkConf conf = new SparkConf().setMaster("local[*]").setAppName("spark");
        final JavaSparkContext jsc = new JavaSparkContext(conf);
        final JavaRDD<Integer> rdd = jsc.parallelize(Arrays.asList(1, 2, 3, 4));

        // 获取系统累加器
        // Spark默认就提供了简单数据聚合的累加器
        LongAccumulator longAccumulator = jsc.sc().longAccumulator("sum");
        //sc.doubleAccumulator
        //sc.collectionAccumulator
        rdd.foreach(longAccumulator::add);

        // 获取累加器的值
        System.out.println("longAccumulator = " + longAccumulator.value());
        jsc.close();
    }
}
